// JavaScript Document
var Layers = Array();
var objCreator;
var bWeResizing = false;
var property;

$(function(){
	$('.button').button();

	$('#container').layout({
		onresize: function(e)
		{
			ohGodResizeEvent();
		}
	});
	
	$("#accordion").accordion({
		fillSpace:	true,
		active:		-1
	});
	$(window).resize(ohGodResizeEvent);
	$("body").resize(ohGodResizeEvent);
	
	
	$("#exportCSVForGARMIN").click(function(e)
	{
		var sURL = 'lib/SearchQuery.php?QueryID=properties&search=' + criteria.search + '&rating=' + criteria.rating;
		sURL +=	'&zip=' + criteria.zip.join(',') + '&city=' + criteria.city.join(',') + '&OutputType=CSV';
		window.open(sURL);
	});
	
	$("#exportKMLForEarth").click(function(e)
	{
	});
	
	$("#edit-mode").button();
});


function InitializeCheckBoxes()
{
	// begin checkbox code
	$.each(['zip', 'city'], function(idx, layertype)
	{
		$("input."+ layertype +"Check").click(function(eventObject){
			if($(this).is(':checked')){
				loadKML(layertype, $(this).attr('value'));
				$.cookie($(this).attr('id'), 'checked', {expires:365});
			}
			else
			{
				unloadKML(layertype, $(this).attr('value'));
				$.cookie($(this).attr('id'), null);
			}
			RefreshData();
		});
	});
	
	$(".layerCheck:checked").each(function(idx, item)
	{
		loadKML($(item).attr('name'), unescape($(item).val()))
	});

	$(".layerCheck").click(function(e)
	{
		var name = $(e.target).attr('name')
		if($(e.target).is(':checked'))
		{
			loadKML($(e.target).attr('name'), unescape($(e.target).val()));
			$.cookie('Layer' + $(e.target).attr('name'), 'checked', {expires:365});
		}
		else
		{
			unloadKML($(e.target).attr('name'));
			$.cookie('Layer' + $(e.target).attr('name'), null);
		}
	});
}

function ohGodResizeEvent()
{
	if(!bWeResizing)
	{
		bWeResizing = true;
		//google.maps.event.trigger(map, "resize");
		$("#accordion").accordion("resize");
		bWeResizing = false;
	}
}

//functions
function loadKML(name, sURL)
{
	var link = g_ge.createLink('');
	link.setHref(sURL);
	var networkLink = g_ge.createNetworkLink('');
	networkLink.set(link, true, false); // Sets the link, refreshVisibility, and flyToView

	g_ge.getFeatures().appendChild(networkLink);
	Layers[name] = networkLink;

}

function unloadKML(name)
{
	if(Layers[name]!=null)
	{
		g_ge.getFeatures().removeChild(Layers[name]);
		Layers[name] = null;
	}
}

function AddToRoute(id, address)
{
	$('#dock li#route ul').append('<li class="routeNode" id="' + id + '">' + address + '</li>');
}

function ExportRoute()
{
	$('#dock li#route ul li.routeNode').each(function(index)
	{
		sOut += "&data[]=" + $(this).attr('id');
	});
	window.open("route.csv.php" + sOut);
}

function hexFromRGB(r, g, b) {
	var hex = [
		r.toString( 16 ),
		g.toString( 16 ),
		b.toString( 16 )
	];
	$.each( hex, function( nr, val ) {
		if ( val.length === 1 ) {
			hex[ nr ] = "0" + val;
		}
	});
	return hex.join( "" ).toUpperCase();
}
function refreshSwatch() {
	var red = $( "#red" ).slider( "value" ),
		green = $( "#green" ).slider( "value" ),
		blue = $( "#blue" ).slider( "value" ),
		hex = hexFromRGB( red, green, blue );
	$( "#swatch" ).css( "background-color", "#" + hex );
	$("input#color").val("#" + hex);
}
